package com.ssm.util;

import java.io.File;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.util.ResourceUtils;

public class MenuHelper {
	@SuppressWarnings("rawtypes")
	public static String getMenu(String filename) {			
		String menu="";			
		try {
            //File file = new File(filename);
			File file = ResourceUtils.getFile(filename);
            SAXReader reader=new SAXReader();
            //读取xml文件到Document中
            Document doc=reader.read(file);
            //获取xml文件的根节点
            Element root=doc.getRootElement();
            menu = "<li class=\"header\">导航栏</li>\r\n";
            int index = 0;
            String active = "active ";
            for(Iterator i = root.elementIterator(); i.hasNext();) {
            	if(index>0) {
            		active="";
            	}
            	Element el = (Element) i.next();
            	menu += "<li class='"+active+"treeview'>\r\n";
            	String url = "#";
            	String icon = el.attribute("icon").getValue();
            	String name = el.attribute("name").getValue();
            	menu +="<a href='"+url+"'>\r\n" + 
            			" <i class='"+icon+"'></i> <span>"+name+"</span>\r\n" + 
            			" <span class='pull-right-container'>\r\n" + 
            			"   <i class='fa fa-angle-left pull-right'></i>\r\n" + 
            			" </span>\r\n" + 
            			"</a>\r\n"; 
            	if(el.attribute("level").getValue().contains("1")) { 
            		menu += "<ul class='treeview-menu'>\r\n";            		
            		for(Iterator it=el.elementIterator();it.hasNext();){
            			Element sel = (Element) it.next();
            			url = sel.attribute("url").getValue();
                    	name = sel.attribute("name").getValue();
                    	menu += "<li><a href='#' action='"+url+"' dataName='"+name+"'><i class='fa fa-circle-o'></i> "+name+"</a></li>\r\n";                         
                    }
            		menu +="</ul>\r\n";
            	}
            	menu += "</li>\r\n";
            	index++;
            }
        } catch (Exception e) {            
            System.out.println(e);
        }
        return menu;
	}	
}
